package com.wink_172.library.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.wink_172.library.callback.ICallback;
import com.wink_172.library.model.Constants;

import java.util.ArrayList;
import java.util.List;


/**
 * textview文字上下滚动控件
 */
public class VerticalTextview extends TextSwitcher implements ViewSwitcher.ViewFactory {

    public static final int FLAG_START_AUTO_SCROLL = 0;
    public static final int FLAG_STOP_AUTO_SCROLL = 1;

    public static final int STATE_PAUSE = 2;
    public static final int STATE_SCROLL = 3;

    public float mTextSize = 16;
    public int mPadding = 5;

    public ICallback m_callback;


    public void setRunning(boolean running) {
        this.running = running;
    }

    public  boolean running = true;  //true正常滚动  false//暂停滚动
    public int textColor = Color.BLACK;

    public int mScrollState = STATE_PAUSE;

    /**
     * @param textSize  textsize
     * @param padding   padding
     * @param textColor textcolor
     */
    public void setText(float textSize, int padding, int textColor) {
        mTextSize = textSize;
        mPadding = padding;
        this.textColor = textColor;
    }

    public OnItemClickListener itemClickListener;
    public Context mContext;
    public int currentId = -1;
    public ArrayList<String> textList;
    public Handler handler;

    public VerticalTextview(Context context) {
        this(context, null);
        mContext = context;
    }

    public VerticalTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        textList = new ArrayList<String>();
    }

    public void setAnimTime(long animDuration_in, long animDuration_out) {
        setFactory(this);
        Animation in = new TranslateAnimation(0, 0, -animDuration_in, 0);
        in.setDuration(animDuration_in);
        in.setInterpolator(new AccelerateInterpolator());
        Animation out = new TranslateAnimation(0, 0, 0, animDuration_out);
        out.setDuration(animDuration_out);
        out.setInterpolator(new AccelerateInterpolator());
        setInAnimation(in);
        setOutAnimation(out);
    }


    /**
     * set time.
     *
     * @param time
     */
    public void setTextStillTime(final long time) {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case FLAG_START_AUTO_SCROLL:
                        if (running) {
                            if (textList.size() > 0) {
                                currentId++;
                                int bbc=currentId % textList.size();
                                setText(textList.get(bbc));

                                m_callback.onSendEvent(Constants.EventEnum.倒计时进行中.ordinal(),bbc);
                            }else {
                                setText("");
                            }
                            handler.sendEmptyMessageDelayed(FLAG_START_AUTO_SCROLL, time);
                        } else {//一直延迟 一直延迟
                            Message new_msg = handler.obtainMessage(msg.what);
                            handler.removeMessages(msg.what);
                            handler.sendMessageDelayed(new_msg, 3000);
                        }

                        break;
                    case FLAG_STOP_AUTO_SCROLL:
                        handler.removeMessages(FLAG_START_AUTO_SCROLL);
                        break;
                }
            }
        };
    }

    /**
     * set Data list.
     *
     * @param titles
     */
    public void setTextList(List<String> titles) {
        textList.clear();
        textList.addAll(titles);
        currentId = -1;
    }

    @Override
    public Handler getHandler() {
        return handler;
    }

    /**
     * start auto scroll
     */
    public void startAutoScroll() {
        mScrollState = STATE_SCROLL;
        if (handler != null) {
            handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL);
        }
    }

    /**
     * stop auto scroll
     */
    public void stopAutoScroll() {
        mScrollState = STATE_PAUSE;
        if (handler != null) {
            handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL);
        }

    }

    @Override
    public View makeView() {
        TextView t = new TextView(mContext);
        t.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        t.setMaxLines(1);
        TextPaint tp = t.getPaint();
        tp.setFakeBoldText(true);//粗体
        t.setPadding(mPadding, mPadding, mPadding, mPadding);
        t.setTextColor(textColor);
        t.setTextSize(mTextSize);
        t.setEllipsize(TextUtils.TruncateAt.END);

        t.setClickable(true);
//        t.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (itemClickListener != null && textList.size() > 0 && currentId != -1) {
//                    itemClickListener.onItemClick(currentId % textList.size());
//                }
//            }
//        });
        return t;
    }

    /**
     * set onclick listener
     *
     * @param itemClickListener listener
     */
    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * item click listener
     */
    public interface OnItemClickListener {
        /**
         * callback
         *
         * @param position position
         */
        void onItemClick(int position);
    }

    public boolean isScroll() {
        return mScrollState == STATE_SCROLL;
    }

    public boolean isPause() {
        return mScrollState == STATE_PAUSE;
    }

    //memory leancks.
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}